package com.e9where.canpoint.customrecycler.recycler;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.e9where.canpoint.customrecycler.recycler.adapter.CustomAdapter;
import com.e9where.canpoint.customrecycler.recycler.callback.SlideCallBcak;
import com.e9where.canpoint.customrecycler.recycler.item.CustomHead;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideCallMode;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideMode;
import com.e9where.canpoint.customrecycler.recycler.onscroll.CustomOnScroll;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2017/12/1.
 * 自定义 CustomRecycler
 */

public class CustomRecycler extends PtrFrameLayout{

    private RecyclerView recyclerView;
    private CustomOnScroll customOnScroll;
    private CustomAdapter customAdapter;
    private SlideMode slideMode;

    public CustomRecycler(Context context) {
        super(context);
        init();
    }


    public CustomRecycler(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomRecycler(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    //初始化
    private void init() {
        //设置下拉的阻尼系数，值越大感觉越难下拉
        setResistance(1.7f);
        //设置超过头部的多少时，释放可以执行刷新操作
        setRatioOfHeaderHeightToRefresh(1.2f);
        //设置下拉回弹的时间
        setDurationToClose(200);
        //设刷新完成，头部回弹时间，注意和前一个进行区别
        setDurationToCloseHeader(1000);
        //设置刷新的时候是否保持头部
        setKeepHeaderWhenRefresh(true);
        //设置下拉过程中执行刷新，我们一般设置为false
        setPullToRefresh(false);
        //ViewPager滑动冲突
        disableWhenHorizontalMove(true);
        //自定义头部
        CustomHead header = new CustomHead(getContext());
        addPtrUIHandler(header);
        setHeaderView(header);



        //创建Recycler对象,并且赋值空间,范围都是上下充满
        recyclerView = new RecyclerView(getContext());
        addView(recyclerView,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    //设置初始的滑动模式
    public void with(SlideMode slideMode, final SlideCallBcak slideCallBcak)
    {
        //上滑
        if (slideMode == SlideMode.BOTH||slideMode == SlideMode.PULL_UP)
        {
            customOnScroll = new CustomOnScroll(getContext(),slideMode,slideCallBcak);
        }
        else
        {
            customOnScroll = new CustomOnScroll(getContext());
        }
        recyclerView.addOnScrollListener(customOnScroll);
        //下拉
        if (slideMode == SlideMode.BOTH || slideMode == SlideMode.PULL_DOWN)
        {
            setPtrHandler(new PtrHandler() {
                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                }

                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    //下拉刷新，关闭没有更多
                    customOnScroll.removeMore();
                    slideCallBcak.onSlideCallBcak(SlideCallMode.PULL_DOWN);
                }
            });
        }
        setSlideMode(SlideMode.NONE);
    }

    public void setSlideMode(SlideMode mode)
    {
        this.slideMode=mode;
        if (customOnScroll!=null)
            customOnScroll.setSlideMode(mode);
        if (slideMode.NONE == slideMode || SlideMode.PULL_UP == slideMode)
        {
            setEnabled(false);
        }
        else
        {
            setEnabled(true);
        }
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        if (adapter==null)
            throw new NullPointerException("adapter not null");
        customAdapter = new CustomAdapter(adapter);
        recyclerView.setAdapter(customAdapter);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layout)
    {
        if (layout==null)
            throw new NullPointerException("adapter not null");
        if (layout instanceof GridLayoutManager)
        {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layout;
            //如果是header或footer，设置其充满整列
            if (customAdapter==null)
                throw new NullPointerException("setLayoutManager ,after setAdapter ");
            gridLayoutManager.setSpanSizeLookup(new CustomSapnSizeLookUp(customAdapter,gridLayoutManager.getSpanCount()));
            recyclerView.setLayoutManager(gridLayoutManager);
        }
        else
        {
            recyclerView.setLayoutManager(layout);
        }
    }

    public void addHead(View view)
    {
        if (customAdapter!=null)
            customAdapter.addHead(view);
    }

    public void removeHead(View view)
    {
        if (customAdapter!=null)
            customAdapter.removeHead(view);
    }

    public void addFoot(View view)
    {
        if (customAdapter!=null)
            customAdapter.addFoot(view);
    }

    public void removeFoot(View view)
    {
        if (customAdapter!=null)
            customAdapter.removeFoot(view);
    }


    //结束
    public void onRefreshCompleted(){
        if (SlideMode.BOTH == slideMode || SlideMode.PULL_DOWN == slideMode)
        {
            refreshComplete();
        }
        if (SlideMode.BOTH == slideMode || SlideMode.PULL_UP == slideMode)
        {
            if (null != customOnScroll)
            {
                customOnScroll.onFinish();
            }
        }
    }


    public void onMore(){
        if (null != customOnScroll)
        {
            customOnScroll.onMore();
        }
    }


    private float y_long;
    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        //下拉的时候不允许上啦 ,是否正在刷新
        if (!isRefreshing())
        {
            switch (e.getAction()){
                case MotionEvent.ACTION_DOWN:
                    y_long = e.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    float currentY = e.getY();
                    if ((currentY - y_long) > 0)
                    {
                        //手指向下
                        if (customOnScroll!=null)
                            customOnScroll.setIs_up(false);
                    }
                    else
                    {
                        if (customOnScroll!=null)
                            customOnScroll.setIs_up(true);
                    }
                    break;
            }
        }
        else
        {
            if (customOnScroll!=null)
                customOnScroll.setIs_up(false);
        }
        return super.dispatchTouchEvent(e);
    }
}
